package cn.pzhu.util;

import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 通过Map实现JavaBean与Json之间的互换
 *
 * Created by dongp on 2016/4/15.
 */
public class JsonHelper {

        /**
         * 将Javabean转换为Map
         *
         * @param javaBean
         *            javaBean
         * @return Map对象
         */
        public static Map toMap(Object javaBean) {

            Map result = new HashMap();
            Method[] methods = javaBean.getClass().getDeclaredMethods();

            for (Method method : methods) {

                try {

                    if (method.getName().startsWith("get")) {

                        String field = method.getName();
                        field = field.substring(field.indexOf("get") + 3);
                        field = field.toLowerCase().charAt(0) + field.substring(1);

                        Object value = method.invoke(javaBean, (Object[]) null);
                        result.put(field, null == value ? "" : value.toString());

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            return result;

        }

        /**
         * 将Json对象转换成Map
         *
         * @param jsonObject
         *            json对象
         * @return Map对象
         * @throws JSONException
         */
        public static Map toMap(String jsonString) throws JSONException {

            JSONObject jsonObject = new JSONObject(jsonString);

            Map result = new HashMap();
            Iterator iterator = jsonObject.keys();
            String key = null;
            String value = null;

            while (iterator.hasNext()) {

                key = (String) iterator.next();
                value = jsonObject.getString(key);
                result.put(key, value);

            }
            return result;

        }

        /**
         * 将JavaBean转换成JSONObject（通过Map中转）
         *
         * @param bean
         *            javaBean
         * @return json对象
         */
        public static JSONObject toJSON(Object bean) {

            return new JSONObject(toMap(bean));

        }

        /**
         * 将Map转换成Javabean
         *
         * @param javabean
         *            javaBean
         * @param data
         *            Map数据
         */
        public static Object toJavaBean(Object javabean, Map data) {

            Method[] methods = javabean.getClass().getDeclaredMethods();
            for (Method method : methods) {

                try {
                    if (method.getName().startsWith("set")) {

                        String field = method.getName();
                        field = field.substring(field.indexOf("set") + 3);
                        field = field.toLowerCase().charAt(0) + field.substring(1);
                        method.invoke(javabean, new Object[] {

                                data.get(field)

                        });

                    }
                } catch (Exception e) {
                }

            }

            return javabean;

        }

        /**
         * JSONObject到JavaBean
         *
         * @param javaBean
         * @return json对象
         * @throws ParseException
         *             json解析异常
         * @throws JSONException
         */
        public static void toJavaBean(Object javabean, String jsonString)
                throws ParseException, JSONException {

            JSONObject jsonObject = new JSONObject(jsonString);

            Map map = toMap(jsonObject.toString());

            toJavaBean(javabean, map);

        }

        public static void writeJson(HttpServletRequest request, HttpServletResponse response, String jsonStr) {
            response.setContentType("text/html");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.print(jsonStr);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}
